﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class ProductRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>, IProductRepository<TItem>
        where TItem : Interfaces.IProduct, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, IAbstractList, new()
    {
        public ProductRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public virtual IList<TItem> GetProductsByCategory(Guid categoryId)
        {
            TItem Product = new TItem();

            var generation = (int)ContentStateService.GetCurrentContentState();

            var query = new Select()
                .From(Product.TableName)
                .InnerJoin(ProductsInCategories.Schema.TableName, ProductsInCategories.Columns.Products, Product.GetSchema().TableName, Product.GetSchema().Columns.GetColumn("NodeId").ColumnName)
                .Where(ProductsInCategories.Columns.Categories).IsEqualTo(categoryId.ToString())
                .And(Product.GetSchema().Columns.GetColumn("Generation")).IsEqualTo(generation)
                .And(Product.GetSchema().Columns.GetColumn("IsActive")).IsEqualTo(true)
                .And(Product.GetSchema().Columns.GetColumn("CultureName")).IsEqualTo(CultureService.GetCurrentCulture().Name)
                .And(ProductsInCategories.Schema.GetColumn(ProductsInCategories.Columns.Generation)).IsEqualTo(ContentState.Draft);

            return query.ExecuteAsCollection<TCollection>();
        }

        public PagedCollection<TItem> PagedWithFilters(CultureInfo culture, ContentState contentState, ActiveStatus status, int pageIndex, int pageSize, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords)
        {
            throw new NotImplementedException();
        }

        public int GetProductCount(CultureInfo culture, ContentState contentState, ActiveStatus status, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords)
        {
            throw new NotImplementedException();
        }
    }

    public class ProductRepository : ProductRepository<BaseProductCollection, BaseProduct>, IProductRepository
    {
        public ProductRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public override IList<BaseProduct> GetProductsByCategory(Guid categoryId)
        {
            var generation = (int)ContentStateService.GetCurrentContentState();

            var query = new Select()
                .From(BaseProduct.Schema)
                .InnerJoin(ProductsInCategories.Schema.TableName, ProductsInCategories.Columns.Products, BaseProduct.Schema.TableName, BaseProduct.Columns.NodeId)
                .Where(ProductsInCategories.Columns.Categories).IsEqualTo(categoryId.ToString())
                .And(BaseProduct.Columns.Generation).IsEqualTo(generation)
                .And(BaseProduct.Columns.IsActive).IsEqualTo(true)
                .And(BaseProduct.Columns.CultureName).IsEqualTo(CultureService.GetCurrentCulture().Name)
                .And(ProductsInCategories.Schema.GetColumn(ProductsInCategories.Columns.Generation)).IsEqualTo(generation);

            return query.ExecuteAsCollection<BaseProductCollection>();
        }

    }

    public interface IProductRepository<TProduct> : IMedianamikRepository<TProduct>
        where TProduct : Interfaces.IProduct
    {
        IList<TProduct> GetProductsByCategory(Guid categoryId);
        PagedCollection<TProduct> PagedWithFilters(CultureInfo culture, ContentState contentState,
            ActiveStatus status, int pageIndex, int pageSize, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords);
        int GetProductCount(CultureInfo culture, ContentState contentState,
            ActiveStatus status, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords);
    }

    public interface IProductRepository : IProductRepository<BaseProduct>
    {
        
    }

}
